package com.shakev.coupon.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shakev.coupon.business.model.CategoryRes;
import com.shakev.coupon.business.service.CategoryBusiService;
import com.shakev.coupon.data.mysql.entity.TCategory;
import com.shakev.coupon.data.mysql.service.ITCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @ClassName CategoryBusiServiceImpl
 * @Description TODO
 * @Author 李伟松
 * @CreateDate 2019-06-05 下午 9:21
 * @UpdateDate 2019-06-05 下午 9:21
 * @Version 1.0
 */
@Service
public class CategoryBusiServiceImpl implements CategoryBusiService {

    @Resource
    ITCategoryService categoryService;

    @Override
    public List<CategoryRes> getCategoryTree(){


        try {//查询所有菜单
            List<CategoryRes> all = this.getAllDto();
            //根节点
            List<CategoryRes> rootCategoryDto = new ArrayList<CategoryRes>();
            for (CategoryRes nav : all) {
                if(nav.getPid()==0){//父节点是0的，为根节点。
                    rootCategoryDto.add(nav);
                }
            }
            /* 根据CategoryDto类的order排序 */
            Collections.sort(rootCategoryDto, order());
            //为根菜单设置子菜单，getClild是递归调用的
            for (CategoryRes nav : rootCategoryDto) {
                /* 获取根节点下的所有子节点 使用getChild方法*/
                List<CategoryRes> childList = getChild(nav.getId(), all);
                nav.setChildren(childList);//给根节点设置子节点
            }
            /**
             * 输出构建好的菜单数据。
             *
             */
            return rootCategoryDto;
        } catch (Exception e) {
            return null;
        }
//		return null;
    }

    public Comparator<CategoryRes> order(){
        Comparator<CategoryRes> comparator = new Comparator<CategoryRes>() {
            @Override
            public int compare(CategoryRes o1, CategoryRes o2) {
                if(o1.getSort() != o2.getSort()){
                    return o1.getSort() - o2.getSort();
                }
                return 0;
            }
        };
        return comparator;
    }

    //	@Cacheable()
    @Override
    public List<TCategory> getAll(){
        QueryWrapper<TCategory> queryWrapper = new QueryWrapper<TCategory>();
        List<TCategory> list = categoryService.list(queryWrapper);
        return list;
    }

    private List<CategoryRes> getAllDto(){
        List<TCategory> list = this.getAll();

        List<CategoryRes> extendsList = new ArrayList<CategoryRes>();
        for (TCategory categoryGoods : list) {
            CategoryRes cate = new CategoryRes(categoryGoods);
            cate.setPic("https://ss0.bdstatic.com/-0U0bnSm1A5BphGlnYG/tam-ogel/f2e1aff288b72ad1a2b4ebec3c44f551_121_121.jpg");
            extendsList.add(cate);
        }
        return extendsList;
    }


    public List<CategoryRes> getChild(Integer id, List<CategoryRes> allCategoryDto){
        //子菜单
        List<CategoryRes> childList = new ArrayList<CategoryRes>();
        for (CategoryRes nav : allCategoryDto) {
            // 遍历所有节点，将所有菜单的父id与传过来的根节点的id比较
            //相等说明：为该根节点的子节点。
            if(nav.getPid().equals(id)){
                childList.add(nav);
            }
        }
        //递归
        for (CategoryRes nav : childList) {
            nav.setChildren(getChild(nav.getId(), allCategoryDto));
        }
        Collections.sort(childList,order());//排序
        //如果节点下没有子节点，返回一个空List（递归退出）
        if(childList.size() == 0){
            return new ArrayList<CategoryRes>();
        }
        return childList;
    }
}
